package com.cn.io.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;


/*
* fileChannel 的 transforFrom() 方法可以将数据重源通道传输到 FileChannel 中（译者注：这个方法在 JDK
* 文档中的解释为将字节从给定的可读取字节通道传输到此通道的文件中）
*
* */
public class TransferFrom {

    public static void main(String[] args) throws IOException {
        // 创建 FileChannel
        RandomAccessFile fromFile = new RandomAccessFile("D:\\opt\\def.txt", "rw");
        FileChannel formChannel = fromFile.getChannel();

        // 创建 FileChannel
        RandomAccessFile toFile = new RandomAccessFile("D:\\opt\\abc.txt", "rw");
        FileChannel toChannel = toFile.getChannel();

        long position = 0;
        long count = formChannel.size();
        toChannel.transferFrom(formChannel, position, count);

        formChannel.close();
        toChannel.close();
        System.out.println("end");
    }
}
